package com.example.tourpal.widget;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;

import com.example.tourpal.PlanOverViewActivity;

public class SpotHorizontalScrollView extends HorizontalScrollView {

	private PlanOverViewActivity activity;
	private ViewGroup child;

	private List<Integer> pointList = new ArrayList<Integer>();

	private int downX = 0;
	private int currentSpot = 0;

	public SpotHorizontalScrollView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public SpotHorizontalScrollView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);

	}

	public SpotHorizontalScrollView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		if (context instanceof PlanOverViewActivity) {
			activity = (PlanOverViewActivity) context;
		}

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);

		child = (ViewGroup) getChildAt(0);
		if (child != null) {
			int subChildCount = child.getChildCount();
			for (int i = 0; i < subChildCount; i++) {
				if (((View) child.getChildAt(i)).getWidth() > 0) {
					pointList.add(((View) child.getChildAt(i)).getLeft());
				}
			}
		}
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {

		downX = (int) ev.getX();
		Log.e("YLQ", "onInterceptTouchEvent:" + downX);
		return true;
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.e("YLQ", "ACTION_DOWN");
			if (downX > 0 && downX < getWidth() / 3) {
				smoothScrollToPreSpot();
				activity.toPreviousPage();
			} else if (downX > getWidth() * 2 / 3 && downX < getWidth()) {
				smoothScrollToNextSpot();
				activity.toNextPage();
			} else {
				smoothScrollToCurrent();
			}
			return true;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			Log.e("YLQ", "ACTION_UP");
		case MotionEvent.ACTION_CANCEL:
			Log.e("YLQ", "ACTION_CANCEL");
			break;
		}
		return super.onTouchEvent(ev);
	}

	private void smoothScrollToCurrent() {
		smoothScrollTo(pointList.get(currentSpot), 0);
	}

	private void smoothScrollToNextSpot() {
		if (currentSpot < child.getChildCount() - 3) {
			SpotGalleryItem oldSpot = (SpotGalleryItem) child
					.getChildAt(currentSpot + 1);
			oldSpot.setGalleryItem(false, true);
			currentSpot++;
			SpotGalleryItem newSpot = (SpotGalleryItem) child
					.getChildAt(currentSpot + 1);
			newSpot.setGalleryItem(true, true);
			smoothScrollTo(pointList.get(currentSpot), 0);
		}
	}

	private void smoothScrollToPreSpot() {
		if (currentSpot > 0) {
			SpotGalleryItem oldSpot = (SpotGalleryItem) child
					.getChildAt(currentSpot + 1);
			oldSpot.setGalleryItem(false, true);
			currentSpot--;
			SpotGalleryItem newSpot = (SpotGalleryItem) child
					.getChildAt(currentSpot + 1);
			newSpot.setGalleryItem(true, true);
			smoothScrollTo(pointList.get(currentSpot), 0);
		}

	}

	public void toNext() {
		smoothScrollToNextSpot();
	}

	public void toPrevious() {
		smoothScrollToPreSpot();
	}

}
